﻿namespace Algoritmia.Problems.Fibonacci
{
    public class SpaceSavingFibonacciNumbers : IFibonacciNumbers
    {
        #region Implementation of IFibonacciNumbers

        public int Number(int n)
        {
            if ( n <= 1)
            {
                return n;
            }
            var prev2 = 0;
            var prev1 = 1;
            int current = 1;
            for(int i = 2; i <= n; i++)
            {
                current = prev2 + prev1;
                prev2 = prev1;
                prev1 = current;
            }
            return current;
        }

        #endregion
    }
}
